package crm.dao.impl;

import crm.Utils.DBUtils;
import crm.Utils.JDBCUtil;
import crm.dao.EmployeeDao;
import crm.entity.Employee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EmployeeSqlSeverImpl extends JDBCUtil implements EmployeeDao {
    @Override
    public int add(Employee employee) {
        String sql = "insert into employee(employee_name,employee_pwd) values(?,?);";
        Object[] params = {employee.getEmployee_name(), employee.getEmployee_pwd()};
        int i = this.executeUpdate(sql, params);
        return i;
    }

    //    注册职员
    @Override
    public boolean findEmp(Employee employee) {
        Boolean flag = false;
        ArrayList<HashMap> list = new ArrayList<>();
        String sql = "select * from employee where employee_name=? and employee_pwd=?";
        Object[] params = {employee.getEmployee_name(), employee.getEmployee_pwd()};
        list = this.executeQuery(sql, params);
        if (list.size() > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean findByName(Employee employee) {
        Boolean flag = false;
        ArrayList<HashMap> list = new ArrayList<>();

        String sql = "select * from employee where employee_name=?";
        Object[] params = {employee.getEmployee_name()};

        list = this.executeQuery(sql, params);
        if (list.size() > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Employee> getByName(String name) {
        String sql = "select * from employee where employee_name=?";
        List<Employee> productlist = new ArrayList<Employee>();

        List<Map<String, Object>> maplist = DBUtils.query(sql, name);
        for (Map<String, Object> map : maplist) {
            Employee employee = new Employee();
            employee.setEmployee_id((Integer) map.get("employee_id"));
            employee.setEmployee_name((String) map.get("employee_name"));
            employee.setEmployee_pwd((String) map.get("employee_pwd"));
            employee.setEmployee_part((String) map.get("employee_part"));
            employee.setProducts_id((Integer) map.get("products_id"));
            employee.setPlan_id((Integer) map.get("plan_id"));
            employee.setEmployee_salary((Integer) map.get("employee_salary"));
            employee.setEmployee_type((String) map.get("employee_type"));
            employee.setPerformance((String) map.get("performance"));
            productlist.add(employee);
        }
        return productlist;
    }


}
